package io.gimo.study.design_pattern.proxy.dynamic_proxy.cglib_proxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class CglibProxy implements MethodInterceptor {

	private Enhancer enhancer = new Enhancer();
	
	public Object getProxy(Class<?> clazz){
		enhancer.setSuperclass(clazz);
		enhancer.setCallback(this);
		return enhancer.create();
	}
	
	/**
	 * 拦截所有目标类调用
	 * obj 目标类的实例
	 * m 目标类方法的反射对象
	 * arg 方法的参数
	 * proxy 代理类的实例
	 */
	@Override
	public Object intercept(Object obj, Method m, Object[] arg, MethodProxy proxy) throws Throwable {
		
		System.out.println("r u ready?");
		//调用被代理类的方法
		proxy.invokeSuper(obj, arg);
		return null;
	}

}
